package com.hiv.mapper;

import com.hiv.common.param.page.PageQuerySamplingBoxParam;
import com.hiv.mapper.domain.SamplingBox;
import java.util.List;
import java.util.Set;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 采样盒子Mapper接口
 *
 * @author zhanghong
 * @date 2022-09-20
 */
@Mapper
@Repository
public interface SamplingBoxMapper {
    SamplingBox selectById(Long id);
    List<SamplingBox> selectList(SamplingBox samplingBox);
    int insert(SamplingBox samplingBox);
    int batchInsert(List<SamplingBox> samplingBoxList);
    int update(SamplingBox samplingBox);
    int deleteById(Long id);
    int deleteByIds(String[] ids);

    @Select({"<script>",
            "   select count(1) from sampling_box",
            "   <where>",
            "       deleted=0",
            "       <if test=\"param.expressStatus!=null\">and express_status=#{param.expressStatus}</if>",
            "       <if test=\"param.hospitalId!=null\">and hospital_id=#{param.hospitalId}</if>",
            "       <if test=\"param.samplerId!=null\">and sampler_id=#{param.samplerId}</if>",
            "       <if test=\"param.expressStatusList != null and param.expressStatusList.size() != 0\">",
            "           and express_status in",
            "           <foreach collection=\"param.expressStatusList\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "               #{item} ",
            "           </foreach> ",
            "       </if> ",
            "   </where>",
            "</script>"})
    long pageCount(@Param("param") PageQuerySamplingBoxParam param);
    @Select({"<script>",
            "   select * from sampling_box",
            "   <where>",
            "       deleted=0",
            "       <if test=\"param.expressStatus!=null\">and express_status=#{param.expressStatus}</if>",
            "       <if test=\"param.hospitalId!=null\">and hospital_id=#{param.hospitalId}</if>",
            "       <if test=\"param.samplerId!=null\">and sampler_id=#{param.samplerId}</if>",
            "       <if test=\"param.expressStatusList != null and param.expressStatusList.size() != 0\">",
            "           and express_status in",
            "           <foreach collection=\"param.expressStatusList\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "               #{item} ",
            "           </foreach> ",
            "       </if> ",
            "   </where>",
            "   order by create_time desc limit #{limit} offset #{offset}",
            "</script>"})
    List<SamplingBox> pageQuery(@Param("param") PageQuerySamplingBoxParam param, @Param("limit") int limit, @Param("offset") long offset);

    @Select("select count(1) from sampling_box where batch_id=#{batchId}")
    Integer countBoxNumByBatchId(Long batchId);

    @Select("select hospital_id from sampling_box where inspection_id=#{inspectionId}")
    Set<Long> queryByInspectionId(Long inspectionId);

    @Select("select inspection_id from sampling_box where hospital_id=#{inspectionId}")
    Set<Long> queryByHospitalId(Long hospitalId);

    @Select({"<script>",
            "   select count(1) from sampling_box",
            "   <where>",
            "       deleted=0",
            "       <if test=\"hospitalIdsSet != null and hospitalIdsSet.size() != 0\">",
            "           and hospital_id in",
            "           <foreach collection=\"hospitalIdsSet\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "               #{item} ",
            "           </foreach> ",
            "       </if> ",
            "   </where>",
            "</script>"})
    Set<Long> queryByHospitalIdList(@Param("hospitalIdsSet") Set<Long> hospitalIdsSet);

    SamplingBox queryBySamplingRecordId(Long samplingRecordId);

    Integer updateExpressStatus(@Param("id") Long id, @Param("expressStatus") int expressStatus);

    List<SamplingBox> listCondition(@Param("param") PageQuerySamplingBoxParam param);

}
